﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO_MechSys;
using System.Drawing;

namespace DAL_MechSys
{
    public class DALCor: DALBase
    {
        public void Inserir(DTOCor dtoCor)
        {
            try
            {
                string storedProssidere = "sp_InserirCor";
                SqlParameter[] parameters =

                {                  
                   new SqlParameter("@componente", dtoCor.Componente),
                   new SqlParameter("@r", dtoCor.R),        
                   new SqlParameter("@g", dtoCor.G),        
                   new SqlParameter("@b", dtoCor.B),        
                   new SqlParameter("@funcionarioid", dtoCor.FuncionarioID),        
                };
                ExecuteNonQuery(storedProssidere, parameters);

            }
            catch (SqlException)
            {
                throw;
            }
        }

        public void Atualizar(DTOCor dtoCor)
        {
            try
            {
                string storedProssidere = "sp_AtualizarCor";
                SqlParameter[] parameters =

                {                  
                   new SqlParameter("@componente", dtoCor.Componente),
                   new SqlParameter("@r", dtoCor.R),        
                   new SqlParameter("@g", dtoCor.G),        
                   new SqlParameter("@b", dtoCor.B),        
                   new SqlParameter("@funcionarioid", dtoCor.FuncionarioID),        
                };
                ExecuteNonQuery(storedProssidere, parameters);

            }
            catch (SqlException)
            {
                throw;
            }
        }

        private DTOCor CriarCor(SqlDataReader sqlDataReader)
        {
            try
            {
                DTOCor dtoCor = new DTOCor();

                if (!string.IsNullOrEmpty(sqlDataReader["r"].ToString()))
                {
                    dtoCor.R = (int)sqlDataReader["r"];
                }
                if (!string.IsNullOrEmpty(sqlDataReader["g"].ToString()))
                {
                    dtoCor.G = (int)sqlDataReader["g"];
                }
                if (!string.IsNullOrEmpty(sqlDataReader["b"].ToString()))
                {
                    dtoCor.B = (int)sqlDataReader["b"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["FuncionarioID"].ToString()))
                {
                    dtoCor.FuncionarioID = (int)sqlDataReader["FuncionarioID"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["componente"].ToString()))
                {
                    dtoCor.Componente = (string)sqlDataReader["componente"];
                }
                dtoCor.Cor = System.Drawing.Color.FromArgb(((int)(((byte)(dtoCor.R)))), ((int)(((byte)(dtoCor.G)))), ((int)(((byte)(dtoCor.B)))));
                return dtoCor;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DTOCor CorPadrao(string componente)
        {
            DTOCor dtoCor = new DTOCor();
            if (componente == "Letra")
            {
                dtoCor.Componente = componente;
                dtoCor.Cor = Color.White;
                dtoCor.R = Color.White.R;
                dtoCor.G = Color.White.G;
                dtoCor.B = Color.White.B;
            }
            else if (componente == "Principal")
            { 
                dtoCor.Componente = componente;
                Color aux = Color.FromArgb(111, 113, 126);
                dtoCor.Cor = aux;
                dtoCor.R = aux.R;
                dtoCor.G = aux.G;
                dtoCor.B = aux.B;
            }
            else if (componente == "Fundo")
            {
                dtoCor.Componente = componente;
                Color aux = Color.FromArgb(240, 240, 240);
                dtoCor.Cor = aux;
                dtoCor.R = aux.R;
                dtoCor.G = aux.G;
                dtoCor.B = aux.B; }
            else
            {
                dtoCor.Componente = componente;
                Color aux = Color.FromArgb(111, 113, 126);
                dtoCor.Cor = aux;
                dtoCor.R = aux.R;
                dtoCor.G = aux.G;
                dtoCor.B = aux.B; }
            return dtoCor;
        }
   
        public DTOCor Buscar(string componente, int funcionarioID)
        {
            DTOCor dtoCor = new DTOCor();

            try
            {
                string storedProssidere = "sp_BuscarCores";

                SqlParameter[] parameters =                
                { 
                   new SqlParameter("@componente", componente),
                   new SqlParameter("@funcionarioid",funcionarioID ),

                };
                using (SqlDataReader sqlDataReader = ExecuteReader(storedProssidere, parameters))
                {
                    while (sqlDataReader.Read())
                    {
                        dtoCor = CriarCor(sqlDataReader);
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
            return dtoCor;
        }

        
    }
}
